/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb.otherUtil;

import com.ejb.GroupsFacade;
import com.ejb.UserFacade;
import com.entity.Groups;
import com.entity.User;
import java.security.NoSuchAlgorithmException;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author jackie
 */
@ManagedBean(name = "register")
@SessionScoped
public class RegisterBean {
    @EJB
    UserFacade userRegister;
    @EJB
    GroupsFacade groupsRegister;
    private String userEmail;
    private String userPassword;
    private String userPassword2;
    private String userFirstName;
    private String userLastName;
    private String userJobTitle;
    
    public String registerToSystem() throws NoSuchAlgorithmException{
            
       
        if(userRegister.find(userEmail)!=null || userEmail == null)
        //if(username.equals("axinggu@gmail.com"))
           return "loginerror?faces-redirect=true";
        else{
          
            DigestByMD5 MD5= new DigestByMD5();

            userPassword = MD5.stringToMD5(userPassword);

           
           User user = new User();
           user.setUserEmail(userEmail);
           user.setUserPassword(userPassword);
           user.setUserFirstName(userFirstName);
           user.setUserLastName(userLastName);
           user.setUserJobTitile(userJobTitle);
           user.setUserCalendar(Boolean.TRUE);
           userRegister.create(user);
           Groups groups  = new Groups();
           groups.setGroupID(groupsRegister.findAll().get(groupsRegister.count()-1).getGroupID()+1);
           groups.setGroupName("user");
           groups.setUserEmail(userRegister.find(userEmail));
           groupsRegister.create(groups);
           return "login?faces-redirect=true";
                   
        }
           
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getUserPassword2() {
        return userPassword2;
    }

    public void setUserPassword2(String userPassword2) {
        this.userPassword2 = userPassword2;
    }

    public String getUserFirstName() {
        return userFirstName;
    }

    public void setUserFirstName(String userFirstName) {
        this.userFirstName = userFirstName;
    }

    public String getUserLastName() {
        return userLastName;
    }

    public void setUserLastName(String userLastName) {
        this.userLastName = userLastName;
    }

    public String getUserJobTitle() {
        return userJobTitle;
    }

    public void setUserJobTitle(String userJobTitle) {
        this.userJobTitle = userJobTitle;
    }

    
}
